package com.yxw.springmvc.userBehavior.validator;

import com.yxw.springmvc.userBehavior.enums.RandomCodeStatEnum;
import com.yxw.springmvc.userBehavior.model.RandomCode;
import com.yxw.springmvc.userBehavior.service.RandomCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by hp on 2016/9/23.
 */
@Service
public class RandomCodeValidatorService {
    @Resource
    private RandomCodeService randomCodeService;

    public boolean checkRandomCodeIsValid(RandomCode randomCode,String randomNum){
        //验证码状态错误
        if (RandomCodeStatEnum.NORMAL.equals(randomCode.getStat())) {
            return false;
        }

        //随机数不正确
        if (!randomNum.equals(String.valueOf(randomCode.getRandomCode()))) {
            return false;
        }

        //随机数已过期
        if (randomCode.getExpiredTime().before(new Date())) {
            return false;
        }

        //更新随机数状态
        randomCode.setStat(RandomCodeStatEnum.EXPIRED.getCode());
        randomCodeService.update(randomCode);
        return true;
    }
}
